package com.dc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@Controller
public class FileController {
    @RequestMapping("/upload")
    @ResponseBody
    public  Map<String ,Object> uploadFile(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
       //获取文件
        MultipartFile file= multipartHttpServletRequest.getFile("file");
        String fileName=file.getOriginalFilename();
        String suffix=fileName.substring(fileName.lastIndexOf("."));
        String newFileName=UUID.randomUUID().toString()+suffix;
        String path=
                request.getSession().getServletContext().getRealPath("/")
                +"upload/"+newFileName;
        System.out.println(path);
        File f=new File(path);
        if(!f.getParentFile().exists()) {
            boolean flag = f.getParentFile().mkdirs();
        }
        FileOutputStream out=new FileOutputStream(path);
        out.write(file.getBytes());
        out.flush();
        out.close();
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("message","上传成功");
        map.put("path",path);
        map.put("oldFileName",fileName);
        map.put("newFileName",newFileName);
        return map;
    }




    @RequestMapping("/uploads")
    @ResponseBody
    public Map<String, Object> uploadFiles(@RequestParam("files")MultipartFile[] tempMFiles,
                                           HttpServletRequest request) throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        if(tempMFiles.length==0||tempMFiles==null){
            resultMap.put("message","文件不能为空");
            return resultMap;
        }
        //记录文件上传的个数

        int fileCount=0;
        List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
        for(MultipartFile file:tempMFiles) {
            if (file.getSize() == 0 || file.isEmpty() || "".equals(file.getName())) {
                continue;
            }
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + suffix;
            String path =
                    request.getSession().getServletContext().getRealPath("/")
                            + "uploads/" + newFileName;
            File f=new File(path);
            if(!f.getParentFile().exists()) {
                boolean flag = f.getParentFile().mkdirs();
            }
            FileOutputStream out=new FileOutputStream(path);
            out.write(file.getBytes());
            out.flush();
            out.close();
            fileCount++;
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("path",path);
            map.put("oldFileName",fileName);
            map.put("newFileName",newFileName);
            list.add(map);
            return map;
        }
        if(fileCount>0){
            resultMap.put("message","上传成功");
            resultMap.put("count",fileCount);
            resultMap.put("list",list);
        }else {
            resultMap.put("message","文件不能为空");
        }
        return resultMap;
    }
}
